/*
 * NodeSearchDialog.java
 *
 * Created on 25 septembre 2007, 18:10
 */

package fr.limsi.seven;

import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 *
 * @author  amazaud
 */
public class NodeSearchDialog extends javax.swing.JDialog
{
	private static NodeSearchDialog singleton = null;
	private int location;
	
	public static NodeSearchDialog getInstance()
	{
		if(singleton == null)
			singleton = new NodeSearchDialog();

		singleton.reset();

		return singleton;
	}

	/** Creates new form BeanForm */
	private NodeSearchDialog()
	{
		super(SevenControlPanel.getInstance(), true);

		initComponents();

		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent evt)
			{
				close();
			}
		});

		KeyAdapter ka = new KeyAdapter(){
			public void keyPressed(KeyEvent evt)
			{
				escape_key(evt);
			}
		};
		
		addKeyListener(ka);
		search_panel.addKeyListener(ka);
		search_label.addKeyListener(ka);
		location_choices.addKeyListener(ka);
		location_label.addKeyListener(ka);
		location_panel.addKeyListener(ka);
		button_panel.addKeyListener(ka);
		search_button.addKeyListener(ka);

		location = Pyramide.SEARCH_IN_LABEL_AND_ID;
	}

	private void reset()
	{
		search_text.setText("");
		pack();

		Dimension screen = getToolkit().getScreenSize();
		setLocation((int)(screen.getWidth() - getWidth()) / 2, (int)(screen.getHeight() - getHeight()) / 2);

		SevenController.getInstance().beginSearch3D();

		setVisible(true);
	}

	 private void close()
	 {
		 SevenController.getInstance().endSearch3D();

		 setVisible(false);
	 }

	private boolean escape_key(KeyEvent evt)
	{
		if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
		{
			close();
			return true;
		}

		return false;
	}
	
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
        // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
        private void initComponents()
        {
                search_panel = new javax.swing.JPanel();
                search_label = new javax.swing.JLabel();
                search_text = new javax.swing.JTextField();
                location_panel = new javax.swing.JPanel();
                location_label = new javax.swing.JLabel();
                location_choices = new javax.swing.JComboBox();
                button_panel = new javax.swing.JPanel();
                search_button = new javax.swing.JButton();

                setTitle("Rechercher");
                search_label.setText("Rechercher :");

                search_text.addKeyListener(new java.awt.event.KeyAdapter()
                {
                        public void keyPressed(java.awt.event.KeyEvent evt)
                        {
                                search_textKeyPressed(evt);
                        }
                });

                javax.swing.GroupLayout search_panelLayout = new javax.swing.GroupLayout(search_panel);
                search_panel.setLayout(search_panelLayout);
                search_panelLayout.setHorizontalGroup(
                        search_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(search_panelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(search_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(search_label)
                                        .addComponent(search_text, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE))
                                .addContainerGap())
                );
                search_panelLayout.setVerticalGroup(
                        search_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(search_panelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(search_label)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(search_text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                );

                location_label.setText("Rercher dans :");

                location_choices.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Labels", "Identifiants", "Tout" }));
                location_choices.setSelectedIndex(2);
                location_choices.addActionListener(new java.awt.event.ActionListener()
                {
                        public void actionPerformed(java.awt.event.ActionEvent evt)
                        {
                                location_choicesActionPerformed(evt);
                        }
                });

                javax.swing.GroupLayout location_panelLayout = new javax.swing.GroupLayout(location_panel);
                location_panel.setLayout(location_panelLayout);
                location_panelLayout.setHorizontalGroup(
                        location_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(location_panelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(location_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(location_label)
                                        .addComponent(location_choices, javax.swing.GroupLayout.Alignment.TRAILING, 0, 268, Short.MAX_VALUE))
                                .addContainerGap())
                );
                location_panelLayout.setVerticalGroup(
                        location_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(location_panelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(location_label)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(location_choices, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                );

                search_button.setText("OK");
                search_button.addActionListener(new java.awt.event.ActionListener()
                {
                        public void actionPerformed(java.awt.event.ActionEvent evt)
                        {
                                search_buttonActionPerformed(evt);
                        }
                });

                javax.swing.GroupLayout button_panelLayout = new javax.swing.GroupLayout(button_panel);
                button_panel.setLayout(button_panelLayout);
                button_panelLayout.setHorizontalGroup(
                        button_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, button_panelLayout.createSequentialGroup()
                                .addContainerGap(108, Short.MAX_VALUE)
                                .addComponent(search_button, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(105, 105, 105))
                );
                button_panelLayout.setVerticalGroup(
                        button_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, button_panelLayout.createSequentialGroup()
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(search_button, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())
                );

                javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
                getContentPane().setLayout(layout);
                layout.setHorizontalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(search_panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(location_panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(button_panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                );
                layout.setVerticalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addComponent(search_panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(location_panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(button_panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                );
        }// </editor-fold>//GEN-END:initComponents

	private void search_textKeyPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event_search_textKeyPressed
	{//GEN-HEADEREND:event_search_textKeyPressed
		if(escape_key(evt))
			return;

		new Thread()
		{
			@Override
			public void run()
			{
				try
				{
					Thread.currentThread().sleep(100);
				}
				catch (InterruptedException ex)
				{
					ex.printStackTrace();
				}
				SevenController.getInstance().search3D(search_text.getText(), location);
			}
		}.start();
	}//GEN-LAST:event_search_textKeyPressed

	private void location_choicesActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_location_choicesActionPerformed
	{//GEN-HEADEREND:event_location_choicesActionPerformed
		switch(location_choices.getSelectedIndex())
		{
			case 0:
				location = Pyramide.SEARCH_IN_LABEL;
				break;
			case 1:
				location = Pyramide.SEARCH_IN_ID;
				break;
			case 2:
				location = Pyramide.SEARCH_IN_LABEL_AND_ID;
				break;
		}
		if(search_text.getText().length() != 0)
			SevenController.getInstance().search3D(search_text.getText(), location);
	}//GEN-LAST:event_location_choicesActionPerformed

	private void search_buttonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_search_buttonActionPerformed
	{//GEN-HEADEREND:event_search_buttonActionPerformed
		close();
	}//GEN-LAST:event_search_buttonActionPerformed
	
	
        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.JPanel button_panel;
        private javax.swing.JComboBox location_choices;
        private javax.swing.JLabel location_label;
        private javax.swing.JPanel location_panel;
        private javax.swing.JButton search_button;
        private javax.swing.JLabel search_label;
        private javax.swing.JPanel search_panel;
        private javax.swing.JTextField search_text;
        // End of variables declaration//GEN-END:variables
	
}
